package com.lsa.skienna.challenge.chap1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.lsa.skienna.challenge.chap1.LCD.Canvas;
import com.lsa.skienna.challenge.chap1.LCD.Command;
import com.lsa.skienna.challenge.chap1.LCD.Digits;
import com.lsa.skienna.challenge.chap1.LCD.Task;

import junit.framework.TestCase;

public class LcdTest extends TestCase {
	public void test() {
		Digits digits = initialize();

		Canvas c = new Canvas();
		c.reset(4, 10);

		for (int i = 9; i >= 0; i--) {
			digits.getDigit(("" + i).toCharArray()[0]).render(c);
			c.nextDigit();
		}

		c.printTo(System.out);
	}

	private Digits initialize() {
		Digits digits = new Digits();

		digits.addDigit('1', new Command[] { Command.WHITESPACE,
				Command.MULTI_WHITESPACE, Command.VCHAR_AND_DOWN,
				Command.LOCAL_CR, Command.WHITESPACE, Command.MULTI_WHITESPACE,
				Command.VCHAR_AND_DOWN });

		digits.addDigit('2', new Command[] { Command.WHITESPACE, Command.HCHAR,
				Command.VCHAR_AND_DOWN, Command.LOCAL_CR, Command.WHITESPACE,
				Command.HCHAR, Command.LOCAL_CR, Command.VCHAR_AND_DOWN,
				Command.WHITESPACE, Command.HCHAR });

		digits.addDigit('3', new Command[] { Command.WHITESPACE, Command.HCHAR,
				Command.VCHAR_AND_DOWN, Command.LOCAL_CR, Command.WHITESPACE,
				Command.HCHAR, Command.VCHAR_AND_DOWN, Command.LOCAL_CR,
				Command.WHITESPACE, Command.HCHAR });

		digits.addDigit('4', new Command[] { Command.CURSOR_DOWN,
				Command.VWALLS, Command.WHITESPACE, Command.HCHAR,
				Command.VCHAR_AND_DOWN });

		digits.addDigit('5', new Command[] { Command.WHITESPACE, Command.HCHAR,
				Command.LOCAL_CR, Command.VCHAR_AND_DOWN, Command.WHITESPACE,
				Command.HCHAR, Command.VCHAR_AND_DOWN, Command.LOCAL_CR,
				Command.WHITESPACE, Command.HCHAR, });

		digits.addDigit('6', new Command[] { Command.WHITESPACE, Command.HCHAR,
				Command.LOCAL_CR, Command.VCHAR_AND_DOWN, Command.WHITESPACE,
				Command.HCHAR, Command.LOCAL_LF, Command.VWALLS,
				Command.WHITESPACE, Command.HCHAR, });

		digits.addDigit('7', new Command[] { Command.WHITESPACE, Command.HCHAR,
				Command.VCHAR_AND_DOWN, Command.VCHAR_AND_DOWN });

		digits.addDigit('8', new Command[] { Command.WHITESPACE, Command.HCHAR,
				Command.LOCAL_LF, Command.VWALLS, Command.WHITESPACE,
				Command.HCHAR, Command.LOCAL_LF, Command.VWALLS,
				Command.WHITESPACE, Command.HCHAR, });
		
		digits.addDigit('9', new Command[] { Command.WHITESPACE, Command.HCHAR,
				Command.LOCAL_LF, Command.VWALLS, Command.WHITESPACE,
				Command.HCHAR, Command.VCHAR_AND_DOWN, Command.LOCAL_CR,
				Command.WHITESPACE, Command.HCHAR, });
		
		digits.addDigit('0', new Command[] { Command.WHITESPACE, Command.HCHAR,
				Command.LOCAL_LF, Command.VWALLS, Command.CURSOR_DOWN,
				Command.VWALLS, Command.WHITESPACE, Command.HCHAR, });
		return digits;
	}
	
	public void test2() throws Throwable {
		LCD.TaskReader taskReader = new LCD.TaskReader(new FileInputStream("data/pc1/lcd/data.txt"));
		Task task = null;
		
		Digits digits = initialize();
		Canvas c = new Canvas();
		
		while( (task = taskReader.readNextTask()) != null) {
			c.reset(task.s, task.chars.length());
			for (int i = 0; i < task.chars.length(); i++) {
				digits.getDigit(task.chars.charAt(i)).render(c);
				c.nextDigit();
			}
			c.printTo(System.out);
		}
	}
}
